package com.ttg.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ttg.contants.TestContants;
import com.ttg.contants.UrlContants;
import com.ttg.pojo.ProfitResultPojo;
import com.ttg.security.EncryptionUtils;
import com.ttg.utils.IdGenerator;
import com.ttg.utils.connection.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/profit_result")
public class ProfitResultDemoController {
    private static final Logger logger = LoggerFactory.getLogger(ProfitResultDemoController.class);


    @RequestMapping
    public String testProfitResult(Model model){
        ProfitResultPojo profitResultPojo = new ProfitResultPojo();
        profitResultPojo.setMerOrderId(IdGenerator.genOrdId16());
        profitResultPojo.setMerchantId(TestContants.MERCHANT_ID);
        profitResultPojo.setBatchNo(IdGenerator.genOrdId16());
        model.addAttribute("profitResult",profitResultPojo);
        return "demo/profit_result_demo";
    }


    @RequestMapping(value = "/do_profit_result",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject doProfitResult(ProfitResultPojo profitResultPojo){
        profitResultPojo.setMerchantId(TestContants.MERCHANT_ID);
        profitResultPojo.setAppId(TestContants.APP_ID);
        profitResultPojo.setSignatureAlgorithm(TestContants.SIGNATUREALGORITHM);
        profitResultPojo.setSignatureInfo(EncryptionUtils.encryption(profitResultPojo.getAllParameters()));
        logger.debug("batch pay result :{}",profitResultPojo);

        String resp = HttpClientUtils.sendPostHttpRequest(UrlContants.PROFIT_RESULT_URL, "POST", profitResultPojo.getAllParameters());
        return JSON.parseObject(resp);
    }

}
